/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client;

import me.l1k3.fx.client.channel.OpacityChannel;
import me.l1k3.fx.client.channel.OpenChannel;
import me.l1k3.fx.client.channel.inter.DimensionConstant.Direction;
import me.l1k3.fx.client.impl.FXShowAndHideAbstract;
import me.l1k3.fx.client.inter.FX;

import com.google.gwt.dom.client.Element;

public class FXReveal extends FXShowAndHideAbstract {
	private OpenChannel openChannel;
	private OpacityChannel opacityChannel;
	
	public FXReveal() {
		this(Direction.VERTICAL);
	}
	
	public FXReveal(Direction direction) {
		switch(direction) {
            case HORIZONTAL:
                openChannel = OpenChannel.horizontal(getType());
            break;
            case BOTH:
                openChannel = OpenChannel.both(getType());
            break;
            default:
                openChannel = OpenChannel.vertical(getType());
            break;
		}
		
		opacityChannel = OpacityChannel.range(getType());
	}
	
	protected FXReveal(Element element, Direction direction) {
	    switch(direction) {
            case HORIZONTAL:
                openChannel = OpenChannel.horizontal(getType());
            break;
            case BOTH:
                openChannel = OpenChannel.both(getType());
            break;
            default:
                openChannel = OpenChannel.vertical(getType());
            break;
        }
	    
	    opacityChannel = OpacityChannel.smartRange(getType(), element);
	    
	    init(element);
    }
    
	//- - - factory
	
	public final static FXReveal reveal(Element element) {
        return FXReveal.reveal(element, Direction.VERTICAL);
    }
	
	public final static FXReveal reveal(Element element, Direction direction) {
	    return new FXReveal(element, direction);
	}
	
    //- - -
	
	@Override
	public boolean isVisible() {
    	return openChannel.isForward();
    }
	
    //- - -
	
	/**
     * @exclude
     */
    @Override
    public FX init(Element element) {
        super.init(element);
        openChannel.init(context);
        validateChannel(openChannel);
        opacityChannel.init(context);
        validateChannel(opacityChannel);
        
        return this;
    }
    
    /**
     * @exclude
     */
	@Override
	public FX toggle() {
		opacityChannel.toggle(context);
		openChannel.toggle(context);
		
		return this;
	}

	/**
     * @exclude
     */
	@Override
	public void onStart() {
	    super.onStart();
		opacityChannel.start(context);
		openChannel.start(context);
	}
	
	/**
     * @exclude
     */
    @Override
    public void onUpdate(double progress) {
        context.setProgress(progress);
        opacityChannel.update(context);
        openChannel.update(context);
    }	
	
    /**
     * @exclude
     */
	@Override
	public void onComplete() {
		opacityChannel.complete(context);
		openChannel.complete(context);
		super.onComplete();
	}
}
